package com.opages.transaction.message.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.opages.transaction.message.api.TransactionMessageApi;
import com.opages.transaction.message.common.domain.HttpResult;
import com.opages.transaction.message.common.exception.MessageException;
import com.opages.transaction.message.model.TransactionMessage;
import com.opages.transaction.message.service.TransactionMessageService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author daizhong.liu
 * @create 2021-11-01 17:42
 **/
public class TransactionMessageController implements TransactionMessageApi {
    @Autowired
    private TransactionMessageService transactionMessageService;

    @Override
    public HttpResult saveMessageWaitingConfirm(TransactionMessage message) throws MessageException {
        try {
            transactionMessageService.saveMessageWaitingConfirm(message);
            return HttpResult.success("预存储消息成功!");
        }catch(Exception e) {
            e.printStackTrace();
            return HttpResult.failed(e.getMessage());
        }
    }

    @Override
    public HttpResult confirmAndSendMessage(String uuid) throws MessageException {
        try {
            transactionMessageService.updateMsgByConfirmAndSendMessage(uuid);
            return HttpResult.success("确认并发送消息成功,uuid=["+uuid+"]");
        }catch (Exception e) {
            e.printStackTrace();
            return HttpResult.failed(e.getMessage());
        }
    }

    @Override
    public HttpResult saveAndSendMessage(TransactionMessage transactionMessage) throws MessageException {
        try {
            transactionMessageService.saveAndSendMessage(transactionMessage);
            return HttpResult.success("存储并发送消息成功!");
        }catch(Exception e) {
            e.printStackTrace();
            return HttpResult.failed(e.getMessage());
        }
    }

    @Override
    public HttpResult directSendMessage(TransactionMessage transactionMessage) throws MessageException {
        try {
            transactionMessageService.directSendMessage(transactionMessage);
            return HttpResult.success("消息发送成功!");
        }catch(Exception e) {
            e.printStackTrace();
            return HttpResult.failed(e.getMessage());
        }
    }

    @Override
    public HttpResult reSendMessage(TransactionMessage transactionMessage) throws MessageException {
        try {
            transactionMessageService.updateMsgByReSendMessage(transactionMessage);
            return HttpResult.success("重发消息成功!");
        }catch(Exception e) {
            e.printStackTrace();
            return HttpResult.failed(e.getMessage());
        }
    }

    @Override
    public HttpResult reSendMessageByMessageId(String messageId) throws MessageException {
        try {
            transactionMessageService.updateMsgReSendMessageByMessageId(messageId);
            return HttpResult.success("重发消息成功,uuid=["+messageId+"]");
        }catch(Exception e) {
            e.printStackTrace();
            return HttpResult.failed(e.getMessage());
        }
    }

    @Override
    public HttpResult setMessageToAreadlyDead(String messageId) throws MessageException {
        try {
            transactionMessageService.updateMessageToAreadlyDead(messageId);
            return HttpResult.success("已将消息标记为死亡,uuid=["+messageId+"]");
        }catch(Exception e) {
            e.printStackTrace();
            return HttpResult.failed(e.getMessage());
        }
    }

    @Override
    public TransactionMessage getMessageByMessageId(String uuid) throws MessageException {
        return transactionMessageService.getMessageByMessageId(uuid);
    }

    @Override
    public HttpResult deleteMessageByMessageId(String messageId) throws MessageException {
        try {
            transactionMessageService.deleteMessageByMessageId(messageId);
            return HttpResult.success("删除消息成功,uuid=["+messageId+"]");
        }catch(Exception e) {
            e.printStackTrace();
            return HttpResult.failed(e.getMessage());
        }
    }

    @Override
    public HttpResult reSendAllDeadMessageByQueueName(String queueName, int batchSize) throws MessageException {
        try {
            transactionMessageService.updateMsgByReSendAllDeadMessageByQueueName(queueName,batchSize);
            return HttpResult.success("重发消息队列["+queueName+"]中的死亡消息,条数["+batchSize+"]");
        }catch(Exception e) {
            e.printStackTrace();
            return HttpResult.failed(e.getMessage());
        }
    }

    @Override
    public List<TransactionMessage> findList(int pageNum, int pageSize, TransactionMessage message) {
        PageHelper.startPage(pageNum,pageSize);
        List<TransactionMessage> list = transactionMessageService.findList(message);
        PageInfo<TransactionMessage> page = new PageInfo<>(list);
        return page.getList();
    }
}
